<?php

namespace App\Models\nursery;

use App\Models\base\traits\HasUserMajor;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\nursery\Major
 *
 * @property int $id
 * @property string $img 专业图
 * @property string $name 名称
 * @property int $sort 排序
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\Models\nursery\UserMajor $user_major
 * @property-read array $majors_sum
 * @property string $detail 服务详情
 * @property string $notice 购买须知
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereNotice($value)
 * @property string $evaluate_tags 评价标签
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\nursery\Major whereEvaluateTags($value)
 */
class Major extends Model
{
    use HasUserMajor;

    protected $fillable = [
        'sort',
        'name',
        'detail',
        'notice',
        'evaluate_tags'
    ];

    /**
     * 获取分类下的育婴师数量
     * @param $value
     * @return array
     */
    public function getMajorsSumAttribute()
    {
        $map['major_id'] = $this->id;
        return UserMajor::where($map)->count();
    }

    public function getEvaluateTagsAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setEvaluateTagsAttribute($value)
    {
        $this->attributes['evaluate_tags'] = json_encode(array_values($value));
    }
}
